package com.gao.modules.depart.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gao.base.Result;
import com.gao.modules.depart.dto.SysDepartDto;
import com.gao.modules.depart.vo.SysDepartVo;
import com.gao.modules.depart.pojo.SysDepart;
import com.gao.modules.depart.service.SysDepartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/depart")
public class DepartController {

    @Autowired
    private SysDepartService sysDepartService;

    @ResponseBody
    @RequestMapping("/test")
    public String test(){
        return "123";
    }

    @ResponseBody
    @PostMapping("/insert")
    public Result insert(@RequestBody SysDepartDto sysDepartDto){
        SysDepart sysDepart = new SysDepart();
        BeanUtils.copyProperties(sysDepartDto,sysDepart);
        SysDepart parentDepart = sysDepartService.getOne(new QueryWrapper<SysDepart>().eq("depart_name", sysDepartDto.getParentName()));
        return sysDepartService.insert(sysDepart,parentDepart);
    }

    @ResponseBody
    @GetMapping("/queryDepartByWant")
    public Result<List<SysDepartVo>> queryDepartByWant(@RequestBody SysDepart sysDepart) {
        List<SysDepart> departs = sysDepartService.queryDepartsByWant(sysDepart);
        ArrayList<SysDepartVo> departsVo = new ArrayList<>();
        for (int i = 0; i < departs.size(); i++) {
            SysDepartVo departVo = new SysDepartVo();
            BeanUtils.copyProperties(departs.get(i), departVo);
            departsVo.add(departVo);
        }
        return Result.success(departsVo);
    }

    @ResponseBody
    @DeleteMapping("/delete")
    //下级部门删除判断
    public Result delete(@RequestParam String departId){
        String childrenIds = "1,2,3,5,8,9";
        String[] split = childrenIds.split(",");
        List<String> strings = Arrays.asList(split);
        List<SysDepart> list = new ArrayList<>();
        for (SysDepart sysDepart : list) {

        }
        for (String string : strings) {

        }
        for (int i = 0; i < strings.size(); i++) {

        }
        return sysDepartService.delete(departId);


    }

    /**
     * 暂时只允许修改部门名称
     * @param sysDepartDto
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    public Result update(@RequestBody SysDepartDto sysDepartDto){
        SysDepart sysDepart = new SysDepart();
        BeanUtils.copyProperties(sysDepartDto,sysDepart);
        return sysDepartService.updateDepart(sysDepart);
    }

}
